using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BuffsListDisplay : MonoBehaviour {
	
	public bool toggled = false;
	
	public List<GameObject> buffDisplays;
	public UITable table;

	// Use this for initialization
	void Start () {
		UIButtonMessage message = gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "ToggleWindow";
		
		buffDisplays = new List<GameObject>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public void ToggleWindow()
	{
		if(!toggled)
		{
			List<BuffDebuff> bl = SheetManager.activeCharacter.buffs;
			
			bl.Sort((x, y) => x.name.CompareTo(y.name));
			
			GameObject go = NGUITools.AddButton(gameObject, 28, "Edit Quick Buffs");
			UIButtonMessage message = go.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "EditQuickBuffList";
			go.transform.localPosition = new Vector3(100, -45, 0);
			buffDisplays.Add(go);
			
			UIDragObject dragger = go.AddComponent<UIDragObject>();
			dragger.target = transform.parent;
			dragger.scale = Vector3.up;
			dragger.restrictWithinPanel = true;
			
			UILabel label;
			
			//Display Buffs that are custom Added Buffs, not from items
			bl.FindAll((b) => SheetManager.activeCharacter._buffs.Contains(b)).ForEach((b) =>
				{
					go = new GameObject();
					go.transform.parent = gameObject.transform;
					go.transform.localScale = Vector3.one;
					go.transform.localPosition = new Vector3(-5, -25 - 90*buffDisplays.Count, 0);
					go.name = b.name;
					buffDisplays.Add(go);
					
					label = NGUITools.AddWidget<UILabel>(go);
					label.text = b.buffDescription;
					label.lineWidth = 100;
					label.transform.localScale = Vector3.one * 18;
					label.transform.localPosition = Vector3.right * 20;
					label.pivot = UIWidget.Pivot.Left;
					label.maxHeight = 100;
					label.RecalcHeight();
					//NGUITools.AddWidgetCollider(label.gameObject);
				
					GameObject activeToggle = NGUITools.AddButton(go, 30, "Active");
					activeToggle.transform.localPosition = Vector3.right * 155;
					activeToggle.GetComponentInChildren<UILabel>().name = "activeMarker";
					if(!b.active)
						activeToggle.GetComponentInChildren<UILabel>().text = "";
				
					NGUITools.AddWidgetCollider(activeToggle);
					message = activeToggle.gameObject.AddComponent<UIButtonMessage>();
					message.functionName = "toggleActiveBuff";
					message.target = gameObject;
				
					NGUITools.AddWidgetCollider(go);
					message = go.gameObject.AddComponent<UIButtonMessage>();
					message.functionName = "openBuffInfo";
					message.target = gameObject;
				
					dragger = go.AddComponent<UIDragObject>();
					dragger.target = transform.parent;
					dragger.scale = Vector3.up;
					dragger.restrictWithinPanel = true;
				}
			);
			
			go = new GameObject();
			go.transform.parent = gameObject.transform;
			go.transform.localScale = Vector3.one;
			go.transform.localPosition = new Vector3(-5, -25 - 90 * buffDisplays.Count, 0);
			buffDisplays.Add(go);
			
			label = NGUITools.AddWidget<UILabel>(go);
			label.text = "Equipment/Ability Buffs: ";
			label.lineWidth = 180;
			label.transform.localScale = Vector3.one * 16;
			label.pivot = UIWidget.Pivot.Left;
			
			dragger = go.AddComponent<UIDragObject>();
			dragger.target = transform.parent;
			dragger.scale = Vector3.up;
			dragger.restrictWithinPanel = true;
			
			//Display Buffs that are from items
			bl.FindAll((b) => !SheetManager.activeCharacter._buffs.Contains(b)).ForEach((b) =>
				{
					go = new GameObject();
					go.transform.parent = gameObject.transform;
					go.transform.localScale = Vector3.one;
					go.transform.localPosition = new Vector3(10, -10 - 90*buffDisplays.Count, 0);
					go.name = b.name;
					buffDisplays.Add(go);
					
					label = NGUITools.AddWidget<UILabel>(go);
					label.text = b.buffDescription;
					label.lineWidth = 150;
					label.transform.localScale = Vector3.one * 18;
					label.pivot = UIWidget.Pivot.Left;
					label.name = "test";
					label.maxHeight = 100;
					label.hasChanged = true;
					NGUITools.AddWidgetCollider(label.gameObject);
					while(label.relativeSize.y > 1f)
					{
						label.transform.localScale /= 1.2f;
					}
				
					//NGUITools.AddWidgetCollider(go);
				
					message = go.gameObject.AddComponent<UIButtonMessage>();
					message.functionName = "openBuffInfo";
					message.target = gameObject;
				
					//dragger = go.AddComponent<UIDragObject>();
					//dragger.target = transform.parent;
					//dragger.scale = Vector3.up;
					//dragger.restrictWithinPanel = true;
				}
			);
			
			table.repositionNow = true;
			toggled = true;
		}
		else
		{
			foreach(GameObject go in buffDisplays)
			{
				Destroy(go);
			}
			
			buffDisplays.Clear();
			
			table.repositionNow = true;
			toggled = false;
		}
	}
	
	void toggleActiveBuff(GameObject target)
	{
		UILabel[] labels = target.transform.GetComponentsInChildren<UILabel>();
		foreach(UILabel l in labels)
		{
			if(l.name == "activeMarker")
			{
				BuffDebuff buff = SheetManager.activeCharacter._buffs.Find(b => b.name == target.transform.parent.gameObject.name);
				if(buff != null)
				{
					buff.active = !buff.active;
					
					if(buff.active)
						SheetManager.activeCharacter.AddNewBuffs(new List<BuffDebuff>{buff});
					
					if(!buff.active)
						l.text = "";
					else
						l.text = "Active";
				}
			}
		}
		
		SheetManager.SaveActiveCharacter(false);
	}
	
	void openBuffInfo(GameObject target)
	{
		GameObject g = Instantiate(GameReference.reference.NewBuffWindowPrefab) as GameObject;
		g.transform.parent = GameReference.reference.BaseMenuPanel;
		g.transform.localScale = Vector3.one;
		BrowserManager.currentNewTransform = g.transform;
		g.transform.localPosition = BrowserManager.CurrentIndex;
		
		BuffEditorMenu w = g.GetComponent<BuffEditorMenu>();
		w.target = gameObject;
		w.functionName = "RemoveInfoTarget";
		w.LoadBuffDisplay(target.gameObject.name, true);
	}
	
	void EditQuickBuffList()
	{
		BrowserManager.CreateBuffsBrowser(gameObject, "SetQuickList", SheetManager.activeCharacter._buffs.ConvertAll<string>(b => b.name), false);
	}
	
	void SetQuickList(List<string> list)
	{
		List<BuffDebuff> removeList = new List<BuffDebuff>();
		
		foreach(BuffDebuff b in SheetManager.activeCharacter._buffs)
		{
			if(!list.Contains(b.name))
				removeList.Add(b);
		}
		
		List<BuffDebuff> buffs = new List<BuffDebuff>();
		
		foreach(string bn in list)
		{
			if(!SheetManager.activeCharacter._buffs.ConvertAll<string>(b => b.name).Contains(bn))
			{
				BuffDebuff buff = BuffDebuffManager.Load(bn);
				SheetManager.activeCharacter._buffs.Add(buff);
				
				buffs.Add(buff);
			}
		}
		
		if(buffs.Count > 0)
			SheetManager.activeCharacter.AddNewBuffs(buffs);
		
		foreach(BuffDebuff b in removeList)
		{
			SheetManager.activeCharacter._buffs.Remove(b);
		}
		
		ToggleWindow();
		ToggleWindow();
		SheetManager.SaveActiveCharacter();
	}
}
